<script setup lang="ts">
import {ref} from 'vue';
import {useRouter} from 'vue-router';

const searchText = ref('');
const activeTags = ref([]);
const activeIndex = ref(0);
const router = useRouter();

const originTags = [
  {
    text: '方向',
    children: [
      {text: 'java', id: 'java'},
      {text: 'c++', id: 'c++'},
      {text: 'python', id: 'python'},
      {text: 'php', id: 'php'},
      {text: 'c#', id: 'c#'},
      {text: '.net', id: '.net'},
    ],
  },
  {
    text: '江苏',
    children: [
      {text: '南京', id: '南京'},
      {text: '无锡', id: '无锡'},
      {text: '徐州', id: '徐州'},
      {text: '南京1', id: '南京1'},
      {text: '无锡1', id: '无锡1'},
      {text: '徐州1', id: '徐州1'},
    ],
  },
];
const tagsTree = ref(originTags)
const onSearch = (val) => {
  tagsTree.value = originTags.map(parentTag => {
        const tempChildren = [...parentTag.children];
        const tempParentTag = {...parentTag};
        tempParentTag.children = tempChildren.filter(item => item.text.includes(searchText.value));
        return tempParentTag;
      }
  )

};
const onCancel = () => {
  activeTags.value = [];
  searchText.value = '';
  tagsTree.value = originTags;
}
const close = (tag) => {
  activeTags.value = activeTags.value.filter(item => item !== tag);
}

const doSearchResult = () => {
  router.push({
    path: '/user/list',
    query: {
      tags: activeTags.value
    }
  })
}
</script>

<template>
  <form action="/">
    <van-search
        v-model="searchText"
        show-action
        placeholder="请输入搜索标签"
        @search="onSearch"
        @cancel="onCancel"
    />
  </form>
  <van-divider content-position="left">已选标签</van-divider>
  <div v-if="activeTags.length === 0 ">
    <van-divider dashed></van-divider>
  </div>
  <van-row gutter="16" justify="center" align="center">
    <van-col v-for="item in activeTags" style="margin-bottom: 10px">
      <van-tag closeable size="medium" type="primary" @close="close(item)">
        {{ item }}
      </van-tag>
    </van-col>
  </van-row>
  <van-divider content-position="left">选择标签</van-divider>
  <van-tree-select
      v-model:active-id="activeTags"
      v-model:main-active-index="activeIndex"
      :items="tagsTree"
  />
  <div style="padding: 16px">
    <van-button block type="primary" @click="doSearchResult">搜索</van-button>
  </div>
</template>

<style scoped>

</style>